package hadoop.mr04;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class person implements WritableComparable<person> {
    String name; // 姓名
    int grader; // 性别
    int sumSource; // 总分

    public person() {
    }

    public person(String name, int grader, int sumSource) {
        this.name = name;
        this.grader = grader;
        this.sumSource = sumSource;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getGrader() {
        return grader;
    }

    public void setGrader(int grader) {
        this.grader = grader;
    }

    public int getSumSource() {
        return sumSource;
    }

    public void setSumSource(int sumSource) {
        this.sumSource = sumSource;
    }

    @Override
    public String toString() {
        String sex = "男";
        if(grader<2){
            sex = "女";
        }
        return name+","+sex+","+sumSource;
    }

    @Override
    public int compareTo(person o) {
        // 按个数降序排序
        return this.sumSource - o.getSumSource()> 0 ? -1 : (this.sumSource-o.getSumSource()<0 ?1:0);
    }

    // 序列化
    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeUTF(name);
        dataOutput.writeInt(grader);
        dataOutput.writeInt(sumSource);
    }

    // 反序列化
    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.name = dataInput.readUTF();
        this.grader = dataInput.readInt();
        this.sumSource = dataInput.readInt();
    }
}
